//
//  NSArray+FFSafeMethod.m
//  fitness-iOS
//
//  Created by 卢仕彤 on 2018/1/15.
//  Copyright © 2018年 卢仕彤. All rights reserved.
//

#import "NSArray+FFSafeMethod.h"

@implementation NSArray (FFSafeMethod)

- (id)ff_safeObjectAtIndex:(NSUInteger)index {
    if (self.count == 0 || index > self.count - 1) {
#ifdef FF_ENABLE_SAFEMETHOD_DEBUG_ASSERT
        NSAssert3(false, @"%@ 数组大小:%tu, 游标%tu越界!", NSStringFromSelector(_cmd), self.count, index);
#else
        NSLog(@"%@ 数组大小:%tu, 游标%tu越界!", NSStringFromSelector(_cmd), self.count, index);
#endif
        return nil;
    }
    return [self objectAtIndex:index];
}

@end

@implementation NSMutableArray (ff_safeMethod)

- (void)ff_safeAddObject:(id)anObject {
    if (anObject) {
        [self addObject:anObject];
    } else {
#ifdef FF_ENABLE_SAFEMETHOD_DEBUG_ASSERT
        NSAssert1(false, @"%@ 空对象!", NSStringFromSelector(_cmd));
#else
        NSLog(@"%@ 空对象!", NSStringFromSelector(_cmd));
#endif
    }
}

- (void)ff_safeRemoveObjectAtIndex:(NSUInteger)index {
    if (self.count == 0 || index > self.count - 1) {
#ifdef FF_ENABLE_SAFEMETHOD_DEBUG_ASSERT
        NSAssert3(false, @"%@ 数组大小:%tu, 游标%tu越界!", NSStringFromSelector(_cmd), self.count, index);
#else
        NSLog(@"%@ 数组大小:%tu, 游标%tu越界!", NSStringFromSelector(_cmd), self.count, index);
#endif
        return;
    }
    [self removeObjectAtIndex:index];
}

- (void)ff_safeRemoveObject:(id)anObject {
    if (!anObject) {
#ifdef FF_ENABLE_SAFEMETHOD_DEBUG_ASSERT
        NSAssert1(false, @"%@ 空对象!", NSStringFromSelector(_cmd));
#else
        NSLog(@"%@ 空对象!", NSStringFromSelector(_cmd));
#endif
        return;
    }
    [self removeObject:anObject];
}

- (void)ff_safeInsertObject:(id)anObject atIndex:(NSUInteger)index {
    if (anObject == nil) {
#ifdef FF_ENABLE_SAFEMETHOD_DEBUG_ASSERT
        NSAssert1(false, @"%@ 空对象!", NSStringFromSelector(_cmd));
#else
        NSLog(@"%@ 空对象!", NSStringFromSelector(_cmd));
#endif
        return;
    }
    if (self.count == 0 || index > self.count - 1) {
        [self addObject:anObject];
        return;
    }
    [self insertObject:anObject atIndex:index];
}

- (void)ff_safeReplaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject {
    if (anObject == nil) {
#ifdef FF_ENABLE_SAFEMETHOD_DEBUG_ASSERT
        NSAssert1(false, @"%@ 空对象!", NSStringFromSelector(_cmd));
#else
        NSLog(@"%@ 空对象!", NSStringFromSelector(_cmd));
#endif
        return;
    }
    if (self.count == 0 || index > self.count - 1) {
#ifdef FF_ENABLE_SAFEMETHOD_DEBUG_ASSERT
        NSAssert3(false, @"%@ 数组大小:%tu, 游标%tu越界!", NSStringFromSelector(_cmd), self.count, index);
#else
        NSLog(@"%@ 数组大小:%tu, 游标%tu越界!", NSStringFromSelector(_cmd), self.count, index);
#endif
        return;
    }
    [self replaceObjectAtIndex:index withObject:anObject];
}

- (void)ff_safeAddObjectsFromArray:(NSArray *)array {
    if (!array) {
#ifdef FF_ENABLE_SAFEMETHOD_DEBUG_ASSERT
        NSAssert1(false, @"%@ 空数组!", NSStringFromSelector(_cmd));
#else
        NSLog(@"%@ 空数组!", NSStringFromSelector(_cmd));
#endif
        return;
    }
    [self addObjectsFromArray:array];
}

- (void)ff_safeRemoveObjectsInArray:(NSArray *)array {
    if (!array) {
#ifdef FF_ENABLE_SAFEMETHOD_DEBUG_ASSERT
        NSAssert1(false, @"%@ 空数组!", NSStringFromSelector(_cmd));
#else
        NSLog(@"%@ 空数组!", NSStringFromSelector(_cmd));
#endif
        return;
    }
    [self removeObjectsInArray:array];
}

@end

